package com.demo.boot.commmon.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.demo.boot.filter.PageCachingFilter;
import com.demo.boot.interceptor.DefaultInterceptor;
import com.opensymphony.module.sitemesh.filter.PageFilter;
import com.opensymphony.module.sitemesh.velocity.VelocityDecoratorServlet;

@SuppressWarnings("deprecation")
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
	/**
	 * @author zhengqiang
	 * @description siteMesh 过滤器
	 * 2017-1-12
	 */
	@Bean
	public FilterRegistrationBean siteMeshFilter(){
		FilterRegistrationBean filter = new FilterRegistrationBean();
		filter.setFilter(new PageFilter());
		return filter;
	}
	
	/**
	 * @author zhengqiang
	 * @description siteMesh servlet
	 * 2017-1-12
	 */
	@Bean
	public ServletRegistrationBean siteMeshServlet(){
		ServletRegistrationBean servlet = new ServletRegistrationBean();
		servlet.setServlet(new VelocityDecoratorServlet());
		servlet.addUrlMappings("*.html");
		return servlet;
	}
	
	
	@Bean
	public PageCachingFilter getPageCachingFilter(){
		return new PageCachingFilter();
	}
	
	
	@Bean
	public FilterRegistrationBean pageCachingFilter(PageCachingFilter cachingfilter){
		FilterRegistrationBean filter = new FilterRegistrationBean();	
		//PageCachingFilter cachingfilter = new PageCachingFilter();
		filter.setFilter(cachingfilter);
		filter.addUrlPatterns("*.html");
		filter.setOrder(0);
		filter.setName("SimplePageCachingFilter");
		return filter;
	}
	
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new DefaultInterceptor());
	}
}
